<?php
namespace app\api\model;
use think\Model;

class Supplier extends Model{
    protected $type = [
        'id'    =>  'integer',       
    ];
    public function type(){
        return $this->hasOne('SupplierType','id','type_id')->bind(['type_name'=>'title']);
    }
    public function operator(){
        return $this->hasOne('manager','id','operator_id')->bind(['operator_name'=>'cn_name']);
    }
    public function supplierList($filter = null){
        $where = [];
        $page = 1;
        $pageCount = 10;
        if(!empty($filter)){
            //指定id查询
            if(!empty($filter['id'])){
                $where['id'] = $filter['id'];
            }
            //模糊查询           
            if(!empty($filter['keyword'])){
                $keyword=trim($filter['keyword']);
                $where['supplier_name|linkman|linkphone'] = ['like',"%{$keyword}%"];               
            }
            //页码            
            $page = !empty($filter['page']) ? $filter['page'] : $page;
            $pageCount = !empty($filter['page_count']) ? $filter['page_count'] : $pageCount;
        }
        $datas = $this->where($where)->with(['type','operator'])->page("$page,$pageCount")->order('id', 'desc')->select();
        $dataCount =  $this->where($where)->count();
        $datalist = array(
            'datas' => $datas,            
            'dataCount' => $dataCount
        );
        return $datalist;
    }

    public function keywordSearch($filter = null){
        $where = [];
        
        if(!empty($filter)){           
            //模糊查询           
            if(!empty($filter['keyword'])){
                $keyword=trim($filter['keyword']);
                $where['supplier_name|linkman|linkphone'] = ['like',"%{$keyword}%"];               
            }           
        }
        $datas = $this->where($where)->with(['type','operator'])->order('id', 'desc')->select();       
        return $datas;
    }
}